<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="100px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="年度:" prop="nd">
            <el-select v-model="queryParams.nd" placeholder="请选择年度" clearable>
              <el-option
                v-for="dict in dict.type.year"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称:" prop="xmName">
            <el-input
              v-model="queryParams.xmName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申报单位:" prop="sbDw">
            <el-input
              v-model="queryParams.sbDw"
              placeholder="请输入申报单位"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目编号:" prop="xmBh">
            <el-input
              v-model="queryParams.xmBh"
              placeholder="请输入项目编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="凭证日期年度:" prop="pzNd">
            <el-select v-model="queryParams.pzNd" placeholder="请选择凭证日期年度" clearable>
              <el-option
                v-for="dict in dict.type.year"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fztz:lstb:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              class="fixedTableTwo" height="300" v-loading="loading" :data="jlBzList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column show-overflow-tooltip label="年度" align="center" prop="nd"/>
      <el-table-column show-overflow-tooltip label="项目编号" align="center" prop="xmBh"/>
      <el-table-column show-overflow-tooltip label="项目名称" align="center" prop="xmName">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.xmName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申报单位" align="center" prop="sbDw"/>
      <el-table-column show-overflow-tooltip label="科目" align="center" prop="km"/>
      <el-table-column show-overflow-tooltip label="凭证日期" align="center" prop="pzTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pzTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="凭证号" align="center" prop="pzh"/>
      <el-table-column show-overflow-tooltip label="凭证种类" align="center" prop="pzZl">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonPzZlClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.pzZl }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="金额" align="center" prop="je"/>
      <el-table-column show-overflow-tooltip label="摘要" align="center" prop="zy"/>
      <el-table-column show-overflow-tooltip label="材料消耗信息(次)" align="center" prop="jlCs">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClXhClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.jlCs }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="vxe-icon-edit"
            @click="handleAddXmLs(scope.row)"
          >流水填报
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 项目流水 -->
    <Xmls ref="xmlS"/>

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <Xmxq ref="xmxq"/>

    <!-- 项目流水详情 -->
    <XmLsXq ref="xmLsXq"/>

    <!-- 材料消耗填报详情 -->
    <ClXhTbXq ref="clXhTbXq"/>

  </div>
</template>

<script>
import Xmls from "@/views/fztz/xmls/index.vue";
import {selectJlBzList} from "@/api/fztz/jlbzcx";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq.vue";
import XmLsXq from "@/views/fztz/xmls/components/XmLsXq.vue";
import ClXhTbXq from "@/views/fztz/clxhtb/components/ClXhTbXq.vue";

export default {
  name: "KyhdjlBzcx",
  components: {ClXhTbXq, XmLsXq, Xmxq, Xmls},
  dicts: ['lstb_xmsszt', 'lstb_zcxx', 'lstb_xmlx', 'year'],
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科研活动记录补正表格数据
      jlBzList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nd: null,
        xmName: null,
        xmBh: null,
        sbDw: null,
        pzNd: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //打开材料消耗填报详情组件
    handleButtonClXhClick(row) {
      this.$refs.clXhTbXq.handleOpen(row)
    },
    //打开项目流水详情组件
    handleButtonPzZlClick(row) {
      this.$refs.xmLsXq.handleOpen(row)
    },
    // 查询项目申报详情
    handleButtonClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    //打开项目流水弹窗页面
    handleAddXmLs(row) {
      this.$refs.xmlS.handleOpen(row)
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询流水填报列表 */
    getList() {
      this.loading = true;
      selectJlBzList(this.queryParams).then(response => {
        this.jlBzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lstbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fztz/lstb/export', {
        ...this.queryParams
      }, `lstb_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
